/*
 * Project: com.mmall.pay.provider.helper
 *
 * File Created at 2019-10-28
 *
 * Copyright 2019 CMCC Corporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ZYHY Company. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license.
 */
package com.mmall.pay.provider.helper;

import com.mmall.pay.api.constant.PayChannel;
import com.mmall.pay.api.constant.PayStatus;
import com.mmall.pay.api.dto.PayApplyDTO;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author jianghucoding
 * @date 2019-10-28
 */
public class PayHelper {

    static Map<Integer, PayService> payServiceMap = new HashMap<>();

    static {
        payServiceMap.put(PayChannel.ALIPAY, new AlipayPay());
        payServiceMap.put(PayChannel.WEIXIN, new WeixinPay());
    }

    public static PayStatus payApply(PayApplyDTO payApplyDTO) {
        int payType = PayChannel.getPayChannel(payApplyDTO.getPayChannel());
        return payServiceMap.get(payType).payApply(payApplyDTO);
    }

    interface PayService {
        PayStatus payApply(PayApplyDTO payApplyDTO);
    }

    static class AlipayPay implements PayService {

        @Override
        public PayStatus payApply(final PayApplyDTO payApplyDTO) {
            // 模拟远程调用支付
            try {
                TimeUnit.MILLISECONDS.sleep(150);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return PayStatus.WAIT;
        }
    }

    static class WeixinPay implements PayService {

        @Override
        public PayStatus payApply(PayApplyDTO payApplyDTO) {
            // 模拟远程调用支付
            try {
                TimeUnit.MILLISECONDS.sleep(150);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return PayStatus.WAIT;
        }
    }
}
